import { Alova } from '@/utils/http/alova/index';

// 人员信息数据结构
interface MemberInfo {
  isPost: string;
  memberId: string;
  memberName: string;
  memberType: string;
  orgArray: string;
  orgArrayName: string;
  orgId: string;
  orgName: string;
  orgSort: number;
  photo: string;
  postType: string;
  sex: string;
  stationId: string;
  userId: string;
  userName: string;
  userRoles: UserRole[];
  isPostName: string;
  memberTypeName: string;
  sexName: string;
  postTypeName: string;
  createTime?: string;
}

// 用户角色数据结构
interface UserRole {
  delFlag: string;
  roleId: string;
  roleLevel: number;
  roleNm: string;
}

// 分页查询结果结构
interface PageResult<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
  orders: any[];
  optimizeCountSql: boolean;
  searchCount: boolean;
  countId: string | null;
  maxLimit: string | null;
  pages: number;
}

const baseApi = '/web/ser/member';

/**
 * 获取人员列表
 */
export function getMemberList(params: any) {
  return Alova.Post<InResult<PageResult<MemberInfo>>>(baseApi + '/listQuery', { params });
}

/**
 * 添加人员
 */
export function addMember(params: Partial<MemberInfo>) {
  return Alova.Post<InResult<MemberInfo>>(baseApi + '/addRecord', { params });
}

/**
 * 更新人员信息
 */
export function updateMember(params: Partial<MemberInfo> & { memberId: string }) {
  return Alova.Post<InResult<MemberInfo>>(baseApi + '/updateRecord', { params });
}

/**
 * 删除人员
 */
export function deleteMember(params: { memberId: string } | { memberIds: string[] }) {
  return Alova.Post<InResult<boolean>>(baseApi + '/deleteRecord', { params });
}

/**
 * 批量删除人员
 */
export function batchDeleteMembers(memberIds: string[]) {
  return Alova.Post<InResult<boolean>>(baseApi + '/batchDeleteRecords', { memberIds });
}

/**
 * 获取人员详情
 */
export function getMemberDetail(memberId: string) {
  return Alova.Post<InResult<MemberInfo>>(baseApi + '/getRecord', { memberId });
}

/**
 * 导出人员列表
 */
export function exportMemberList(params: any) {
  return Alova.Post<InResult<string>>(baseApi + '/exportRecords', { params });
}

/**
 * 导入人员数据
 */
export function importMembers(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return Alova.Post<InResult<{ successCount: number; failCount: number; errorMsgs: string[] }>>(baseApi + '/importRecords', formData);
}

/**
 * 验证人员信息
 */
export function validateMember(params: Partial<MemberInfo>) {
  return Alova.Post<InResult<boolean>>(baseApi + '/validateRecord', { params });
}

/**
 * 启用/禁用人员账号
 */
export function toggleMemberStatus(params: { memberId: string, status: string }) {
  return Alova.Post<InResult<boolean>>(baseApi + '/toggleStatus', { params });
}

/**
 * 重置人员密码
 */
export function resetMemberPassword(memberId: string) {
  return Alova.Post<InResult<boolean>>(baseApi + '/resetPassword', { memberId });
}

/**
 * 获取人员角色列表
 */
export function getMemberRoles(memberId: string) {
  return Alova.Post<InResult<UserRole[]>>(baseApi + '/getRoles', { memberId });
}

/**
 * 分配人员角色
 */
export function assignMemberRoles(params: { memberId: string, roleIds: string[] }) {
  return Alova.Post<InResult<boolean>>(baseApi + '/assignRoles', params);
}